﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Workflow.Form;
using Jzy.Workflow.Task;
using Frame.Lib.Utilities;
using Frame.Lib.Common;


namespace Jzy.Web.WorkFlow.Fore
{
    public partial class MistakeList : PageList
    {
        private FlowTaskBO _task = new FlowTaskBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();
            }
            this.InitSelectID(hidSelectIDs);
            JSUtil.ClickBtn(lnkRecycle, btnRecycle);
            JSUtil.DelConfirm(btnRecycle, "回收该表单");
        }

        private void BindView()
        {
            this.InitLV(lv, lvHeader);
            lv.EnableViewState = true;
            IEnumerable<FlowTaskInfo> tasks = _task.GetCanRecycleList(AppSession.UserID);
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = tasks;
            lv.DataBind();
        }

        protected FlowFormInfo GetForm(object form)
        {
            return (FlowFormInfo)form;
        }

        protected void btnRecycle_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                List<string> ids = StrUtil.SplitToStr(strSelectIDs);
                int successCount = 0;
                int failureCount = 0;
                foreach (string id in ids)
                {
                    bool success = _task.Recycle(id);
                    if (success)
                    {
                        successCount++;
                    }
                    else
                    {
                        failureCount++;
                       
                    }
                }

                string msg = null;

                if (successCount > 0 && failureCount > 0)
                {
                    msg = "回收成功" + successCount + "条,如果你是拟稿人请去草稿箱中查找,否则请在待办箱中查找\\r\\n" + "回收失败" + failureCount + "条,可能已被下一节点审批";
                }
                else if (failureCount > 0)
                {
                    msg = "回收成功,如果你是拟稿人请去草稿箱中查找,否则请在待办箱中查找";
                }
                else
                {
                    msg = "回收失败,可能已被下一节点审批";
                }

                JSUtil.Alert(msg);

                BindView();
            }

        }

        protected void lv_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            bool success = _task.Recycle(e.CommandArgument);
            BindView();
            if (success)
            {
                JSUtil.Alert("回收成功,如果你是拟稿人请去草稿箱中查找,否则请在待办箱中查找");
            }
            else
            {
                JSUtil.Alert("回收失败，可能已被下一节点审批");
            }
        }
    }
}
